<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/7/27/027
 * Time: 8:57
 */

namespace App\Services\im;



use Illuminate\Support\Facades\DB;
use RongCloud\RongCloud;

class ImService
{

    private static $instance;

    private function __construct()
    {
        self::$instance = self::getInstance();
    }

    public static function getInstance()
    {
        if (!self::$instance){
            self::$instance = new RongCloud(config('rongyun.appKey'),config('rongyun.appSecret'));
        }
        return self::$instance;
    }

    /**
     * Created by xiaosong
     * E-mail:4155433@gmail.com
     * @param $user_id
     * @param bool $is_back 是否为后台管理员获取token
     * @return string
     */
    public static function getToken($user_id,bool $is_back = false) :string
    {
        if ($is_back){
            $userInfo  = (array) DB::table('admin_users')->where('id',$user_id)->select('id','name','avatar as portrait')->first();
            $userInfo['id'] = 'admin'.$userInfo['id'];
        }else{
            $userInfo  = (array) DB::table('users')->where('id',$user_id)->select('id','name','header_img as portrait')->first();
        }
        if (empty($userInfo['name'])) $userInfo['name'] = '游客'.rand(111111,999999);
        if (empty($userInfo['portrait'])) $userInfo['portrait'] = config('app.default_header');

        $result = self::getInstance()->getUser()->register($userInfo);

        if ($result['code'] == 200){
            return $result['token'];
        }
        return "";
    }





}